from django.shortcuts import render
from rest_framework.viewsets import ReadOnlyModelViewSet,ModelViewSet
from rest_framework import permissions
from rest_framework.response import Response
from accounts.filters import UserFilter
from .serializers import *
from .models import *
# Create your views here.
class ProductViewSet(ReadOnlyModelViewSet):
    queryset = Product.objects.all()
    serializer_class = ProductSerializer

class ShopCartViewSet(ModelViewSet):
    queryset = ShopCart.objects.all()
    serializer_class = ShopCartSerializer

class ShopOrderViewSet(ModelViewSet):
    queryset = ShopOrder.objects.all()
    serializer_class = ShopOrderSerializer
    permission_classes = (permissions.IsAuthenticated,)
    filter_backends = (UserFilter,)

    def create(self, request, *args, **kwargs):
        serializer = self.get_serializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        self.perform_create(serializer)
        serializer = ShopOrderOutSerializer(serializer.instance)
        headers = self.get_success_headers(serializer.data)
        return Response(serializer.data, status=201, headers=headers)